import 'package:flutter/cupertino.dart';
import 'package:flutter_09_game/base/controller/base_controller.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/user/model/model_user_wallet.dart';
import 'package:flutter_09_game/utils/loading/loading_utils.dart';
import 'package:flutter_09_game/widgets/verify_input_box/src/verification_box.dart';
import 'package:get/get.dart';

class PromotionTransferCommissionLogic extends BaseController {
  // List<String> options = ['USDT', "TRX"];
  List<String> options =
      AppUserManager().walletList.map((item) => item.symbol ?? '').toList();

  late var selectorTagRx = options.first.obs;

  ModelUserWallet? currentWallet;

  TextEditingController editingController = TextEditingController();
  VerificationBoxController verificationBoxController =
      VerificationBoxController();
  RxString fundPassword = ''.obs;

  // RxString confirmPassword = ''.obs;
  RxBool isFormValid = false.obs;

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    getWalletData();
    ever(fundPassword, (val) {
      validateForm();
    });

    ever(selectorTagRx, (val) {
      currentWallet = AppUserManager()
          .walletList
          .firstWhereOrNull((item) => item.symbol == selectorTagRx.value);
      update();
    });
  }

  getWalletData() async {
    if (AppUserManager().walletList.isEmpty) {
      await AppUserManager().loadWalletData();
    }
    if (AppUserManager().walletList.isEmpty) {
      Get.back();
    } else {
      currentWallet = AppUserManager().walletList.first;
    }
  }

  void validateForm() {
    double amount = double.tryParse(editingController.text.trim()) ?? 0.0;
    final isValid = amount > 0 && fundPassword.value.length == 6;
    isFormValid.value = isValid;
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    editingController.dispose();
  }

  postWalletRebateToBalance() async {
    HttpResponse response =
        await HttpManager().post(API.postWalletRebateToBalance,
            // fromJson: .fromJson,
            params: {
              'amount':
                  ((double.tryParse(editingController.text) ?? 0) * 1000000)
                      .toInt(),
              'fund_password': fundPassword.value,
              'wallet_id': currentWallet?.id
            },
            uiType: HttpUIType.loadingAndToast);
    if (response.isSuccess) {
      LoadingUtils.showToast(LocaleKeys.convertSuccess.tr);
      await AppUserManager().loadWalletData();
      currentWallet = AppUserManager()
          .walletList
          .firstWhereOrNull((item) => item.symbol == selectorTagRx.value);
      update();
      editingController.text = '';
      fundPassword.value = '';
      verificationBoxController.text = '';
      // return response.data;
    }
  }
}
